package com.zzyl.nursing.controller.member;

import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.ReservationService;
import com.zzyl.nursing.vo.ReservationResidueNumVo;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import static com.zzyl.common.core.domain.AjaxResult.success;

@RestController
@RequestMapping("/member/reservation")
public class ReservationController extends BaseController {
    @Autowired
    private ReservationService reservationService;
    @GetMapping("/cancelled-count")
    public AjaxResult getCount(){
        Integer count = reservationService.getCount();
        return success(count);
    }
    @GetMapping("/countByTime")
    public R<List<ReservationResidueNumVo>> getSurplusNum(){
        List<ReservationResidueNumVo> list = reservationService.getSurplusNum();
        return R.ok(list);
    }

    @PostMapping
    public AjaxResult save(@RequestBody Reservation reservation){
        reservation.setStatus(0);
        reservationService.save(reservation);
        return success();
    }

    @GetMapping("/page")
    public R<TableDataInfo> page(Integer status){
        startPage();
        List<Reservation> list = reservationService.lambdaQuery()
                    .eq(status != null,Reservation::getStatus,status)
                    .list();
        return R.ok(getDataTable(list));
    }

    @PutMapping("/{id}/cancel")
    public AjaxResult cancel(@PathVariable Long id){
        reservationService.lambdaUpdate().set(Reservation::getStatus,2).eq(Reservation::getId,id).update();
        return success();
    }

}
